#!/usr/bin/php
<?php
require_once 'Zend/Console/Getopt.php';
require_once 'Zend/Filter/Word/DashToCamelCase.php';

class KajoaDev
{
    protected $_options;
    
    protected function _api()
    {
        $rootPath   = dirname(__FILE__) . '/..';
        
        $arguments  = " --filename $rootPath/library/*.php";
        $arguments .= ' --output HTML:frames:earthli';
        $arguments .= " --target $rootPath/dev-tools/temp/documentation/api";
        
        if (!isset($this->_options->quiet)) {
            $arguments .= ' -q'; 
        }

        $command = "phpdoc $arguments";
        return $this->_executeCommand($command);
    }    
    
    protected function _check()
    {
        $arguments  = ' --standard=Zend';
        $arguments .= ' --extensions=php';
        $arguments .=  '--ignore=*/Kajoa/View/Smarty/Helpers/*';
        
        if (!isset($this->_options->warning)) {
            $arguments .= ' -n'; 
        }
        
        $path    = dirname(__FILE__) . '/../library';
        $command = "phpcs $arguments $path";
        return $this->_executeCommand($command);
    }
    
    protected function _executeCommand($command)
    {
        exec($command, $output, $returnValue);
        
        if (!isset($this->_options->quiet)) {
            echo implode(PHP_EOL, $output) . PHP_EOL;
        }
        
        return $returnValue;
    }

    protected function _getMethodName($argument)
    {
        $filter = new Zend_Filter_Word_DashToCamelCase();
        $value  = $filter->filter($argument);
        
        $methodName = '_' . strtolower($value[0]) . substr($value, 1);
        return $methodName;
    }

    protected function _help()
    {
        self::displayUsage();
    }

    protected function _test()
    {
        $path    = dirname(__FILE__) . '/../tests/AllTests.php';
        $command = 'phpunit AllTests ' . $path;
        return $this->_executeCommand($command);
    }
    
    protected function __construct(Zend_Console_Getopt $options)
    {
        $this->_options = $options;
    }

    public static function displayUsage()
    {
        $usage  = "\n";
        $usage .= "Usage: kajoa-dev [options] command\n";
        $usage .= "\n";
        $usage .= "Commands:\n";
        $usage .= "\tapi\t\tBuild API documentation\n";
        $usage .= "\tcheck\t\tCheck coding standard\n";
        $usage .= "\ttest\t\tRun unit tests\n";
        $usage .= "\n";
        $usage .= "Options:\n";
        $usage .= "\t-q, --quiet\tDo not display anything\n";
        $usage .= "\t-w, --warning\tInclude warnings in 'check' command\n";
        $usage .= "\n";
        echo $usage;
    }

    public static function run()
    {
        try {
            $config  = array(
                'quiet|q'   => 'Quiet',
                'warning|w' => 'Warning',    
            );
            $options = new Zend_Console_Getopt($config);
            
            $remainingArguments = $options->getRemainingArgs();
            if (1 != count($remainingArguments)) {
                $options->getUsageMessage();
                exit;
            }

            $instance = new self($options);
            $method  = $instance->_getMethodName($remainingArguments[0]);
            if (method_exists('KajoaDev', $method)) {
                return $instance->$method();
            } else {
                self::displayUsage();
                exit;
            }             
        } catch (Zend_Console_Getopt_Exception $exception) {
            self::displayUsage();
            exit;
        }            
    }
}

KajoaDev::run();